# template.py
# by Sean A. Harrison <sah@tyndale.com>
# ---------------------------------------------------------------------------
# $Id: template.py 1 2012-07-07 03:43:29Z sah.harrison@gmail.com $

"""A simple templating engine that stays out of your way.
   Designed to be small, light, and fast.

Requirements: A fast, lightweight, embedded Python system.
The full Python language must be available. Memory usage must be small.
---------------------------------------------------------------------------
Memory Usage                                  Memory  Footprint  Notes
                                              ------  ---------  -----
> python                                      1856 K
and then *one* of the following:
   from string import Template                1976 K    120 K    for cp.
   from amplitude.template import Template    1944 K     88 K
   from evoque.domain import Domain           2888 K   1032 K
   import tempita                             3360 K   1504 K
   from mako.template import Template         3816 K   1960 K
   from Cheetah.Template import Template      5992 K   4136 K    proj dead
---------------------------------------------------------------------------

Examples:
>>> t = Template("Some math: a + b = {{a+b}}. Hi, {{name}}!")
>>> t.render(a=5, b=7, name='Sean')
'Some math: a + b = 12. Hi, Sean!'
"""

import re

# ---------------------------------------------------------------------------
class Template(str):
    """An eval-based template that uses {{expression}} syntax; the expression can be a variable that is 
    passed to the template when Template.render(**args) is called, or it can be a method that is defined
    in the Template class. To expand the list of methods that are defined, subclass the Template class.
    """

    def render(self, **args):
        """render the template with the given args, plus the functions available below"""
        return self.__str__()
        
    # methods that are enabled within a template
    
    def h(self, body):
        """HTML-escape body"""
        pass
        
    def t(self, body, **args):
        """render body as a template with args."""
        pass

    def cond(self, conds=[]):
        """like lisp cond: A list of condition-body pairs, evaluate the first one that evaluates True. 
        The body of each pair is evaluated as a template in the current namespace."""
        pass
    
    def for_(self, init_clause, body):
        """for-loop as a function, body is evaluated as a template in the current namespace"""
        pass
        
    def def_(self, definition, body):
        """define a template function, with the body to be evaluated in current namespace."""
        pass
        
    def block(self, definition, body):
        """like def_(), but it also renders the template function in the current location."""
        pass
        
    def include(self, path):
        """include and evaluate as a template the contents of path"""
        pass
        
if __name__ == "__main__":
    import doctest
    doctest.testmod()
